package com.aurora.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.binary.Hex;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Log4j2
public class FileUtil {

    public static String getMd5(InputStream inputStream) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            byte[] buffer = new byte[8192];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                md5.update(buffer, 0, length);
            }
            return new String(Hex.encodeHex(md5.digest()));
        } catch (Exception e) {
             log.error("未知异常  ",e);
            return null;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                 log.error("未知异常  ",e);
            }
        }
    }

    public static String getExtName(String fileName) {
        if (StringUtils.isBlank(fileName)) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }

    public static File multipartFileToFile(MultipartFile multipartFile) {
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] filename = Objects.requireNonNull(originalFilename).split("\\.");
            file = File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
             log.error("未知异常  ",e);
        }
        return file;
    }


    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2048) {
            accuracy = 0.6;
        } else if (size < 3072) {
            accuracy = 0.44;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }

    private static final String BASE_URL = "https://blog.csdn.net/nav/advanced-technology";
    private static final int PAGE_SIZE = 20;

    public static void main(String[] args) {
        List<String> articleUrls = new ArrayList<>();
        for (int page = 1; page <= 1; page++) {
            String url = BASE_URL + "/" + page;
            try {
                Document doc = Jsoup.connect(url).get();
                Elements articleElements = doc.select(".article-list > .article-item-box > h4 > a");
                List<String> pageArticleUrls =
                        articleElements.stream().map(el -> el.attr("href")).collect(Collectors.toList());
                articleUrls.addAll(pageArticleUrls);
            } catch (IOException e) {
                System.err.println("Error fetching URL: " + url);
                 log.error("未知异常  ",e);
            }
        }

        for (String articleUrl : articleUrls) {
            try {
                Document doc = Jsoup.connect(articleUrl).get();
//                Element articleElement = doc.selectFirst("article");
//                String title = articleElement.selectFirst(".title-article").text();
//                String content = articleElement.selectFirst(".htmledit_views").html();
//                System.out.println("Title: " + title);
//                System.out.println("Content: " + content);
//                System.out.println();
            } catch (IOException e) {
                System.err.println("Error fetching URL: " + articleUrl);
                 log.error("未知异常  ",e);
            }
        }
    }
}
